package cn.kgc.crowd1048.fundingadmin.controller;


import cn.kgc.crowd1048.fundingadmin.entity.TAdmin;
import cn.kgc.crowd1048.fundingadmin.service.TAdminService;
import cn.kgc.crowd1048.fundingadmin.util.CrowdFundingConstant;
import cn.kgc.crowd1048.fundingadmin.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czj
 * @since 2024-04-17
 */
//@RestController
//@RequestMapping("")
@Controller
public class TAdminController {
    @Autowired
    TAdminService adminService;
//    查询全部的方法
//    @RequestMapping("aa")
//    public List aa(){
//        List<TAdmin> list = adminService.list(null);
//        return list;
//    }

//    登录
    @RequestMapping("/admin/to/login/page.html")
    public String toLoginPage(){
        return "admin-login";
    }
    @RequestMapping("/admin/do/login")
    public String doLogin(
            @RequestParam("loginAcct")String loginAcct,
            @RequestParam("userPswd")String userPswd,
            Model model,
            HttpSession session
    ){
        TAdmin admin = adminService.login(loginAcct, userPswd);
        if (admin.getUserName()==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            System.out.println("admin = " + admin);
            return "admin-login";
//            return "admin-main";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        return "main";
    }


//    退出登录
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session){
        session.invalidate();
//        if (session.getAttribute())
        return "redirect:/index";
    }


    @RequestMapping("/aa")
    public String logout2(){

        return "aa";
    }

//    用户维护
    @RequestMapping("admin/query/for/search")
    public String queryForSearch(
    @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
    @RequestParam(value = "pageSize",defaultValue = "3",required = false)Integer pageSize,
    @RequestParam(value = "keyword",defaultValue = "",required = false)String keyword,
    Model model
    ){
        PageInfo<TAdmin> pageInfo = adminService.queryForkeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin-page";
    }


    //批量删除 + 单条查询  都访问这个
    @ResponseBody
    @PostMapping("admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminList){
        adminService.batchRemove(adminList);
        try{
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            return ResultEntity.failed(null,e.getMessage());
        }
    }
//    跳转到添加的页面
    @RequestMapping("/admin/to/add/page.html")
    public String tiaozhuan(){
        return "admin-add";
    }

//    添加的方法
@PostMapping("admin/save")
    public String saveAdmin( TAdmin tAdmin){
    System.out.println("tAdmin = " + tAdmin.toString());
        adminService.saveAdmin(tAdmin);
        return "redirect:/admin/query/for/search";
}

//修改前查询的方法
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId,Model model){
        TAdmin admin = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);
        return "admin-edit";
    }

//    修改的id
    @RequestMapping("admin/update")
    public String updateAdmin(TAdmin tAdmin,Integer pageNum){
        try {
            adminService.updateAdmin(tAdmin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum="+pageNum;

    }




}

